<template>
  <div class="app-container">
    <div class="project-list-view">
      <el-button type="primary">发布公告</el-button>
      <div style="margin-top: 10px;"></div>
      <el-table :data="tableData3"
                border
                style="width: 100%"
                :row-key="tableData3.pro_id">
        <el-table-column prop="id"
                         label="ID"
                         width="50">
        </el-table-column>
        <el-table-column prop="title"
                         label="公告标题">
        </el-table-column>
        <el-table-column prop="content"
                         label="公告内容">
        </el-table-column>
        <el-table-column prop="delete"
                         label="删除"
                         width="80">
          <template slot-scope="scope">
            <el-button @click="handleDelete(scope.row.pro_id)"
                       type="danger"
                       plain
                       size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {
  getProjectList,
  updateProjectSelected,
  updateProjectHot,
  delProject,
} from '@/api/projectList'
export default {
  name: 'ProjectList',
  computed: {},
  data() {
    return {
      tableData3: [
        {
          id: 1,
          title: '这是一则公告',
          content: '管理员发布了一则公告',
        },
        {
          id: 2,
          title: '这是一则公告',
          content: '管理员发布了两则公告',
        },
        {
          id: 3,
          title: '这是一则公告',
          content: '管理员发布了三则公告',
        },
        {
          id: 4,
          title: '这是一则公告',
          content: '管理员发布了四则公告',
        },
      ],
      projectSelected: '0',
      projectHot: '0',
    }
  },
  methods: {
    // 获取项目信息
    getProjectInfo: function () {
      return getProjectList().then((res) => {
        this.tableData3.splice(0, this.tableData3.length)
        this.tableData3.push(...res.data.rows)
      })
    },
    // 根据项目文件地址下载项目文件
    handleDownloadProject: function (url) {
      console.log('download', JSON.parse(url))
    },
    // 项目设置按钮，初始化内部所需参数
    handleSetting: function (info) {
      let selected = info.selected.toString()
      let hot = info.hot.toString()
      this.projectSelected = selected
      this.projectHot = hot
    },
    // 删除项目
    handleDelete: function (id) {
      console.log('删除')
      delProject({ id: id }).then((res) => {
        if (res.data.status === 200) {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'success',
          }),
            this.getProjectInfo()
        }
      })
    },
    // 项目精选开关
    handleChangeSelected: function (projectId) {
      updateProjectSelected({
        pro_id: projectId,
        selected: this.projectSelected,
      }).then((res) => {
        if (res.data.status == 200) {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'success',
          }),
            this.getProjectInfo()
        } else {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'warning',
          })
        }
      })
    },
    // 项目热门开关
    handleChangeHot: function (projectId) {
      updateProjectHot({
        pro_id: projectId,
        hot: this.projectHot,
      }).then((res) => {
        if (res.data.status == 200) {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'success',
          }),
            this.getProjectInfo()
        } else {
          this.$message({
            showClose: true,
            message: res.data.data,
            type: 'warning',
          })
        }
      })
    },
  },
  // beforeMount() {
  //   this.getProjectInfo()
  // },
}
</script>

<style lang="scss"></style>